package com.onenine.app.utils;

import android.content.res.TypedArray;
import android.support.annotation.ArrayRes;
import android.text.TextUtils;

import com.onenine.app.R;
import com.onenine.app.model.CoterieItem;
import com.onenine.app.response.UserInfoResponse;
import com.onenine.chat.rongcloud.RcUser;
import com.onenine.common.util.ApUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间：2018/8/1 0001 9:59
 * 创建人：fly
 * 功能描述：通用工具类
 */
public class CHelper {

    public static final String PZH = "--";
    //阿里云oss的前缀
    public static final String OSS_IMAGE_PREFIX = "oss.19quapp.com";
    //阿里云旧版oss的前缀
    public static final String OSS_IMAGE_PREFIX_OLD = "onenine.oss";
    //微信头像前缀
    public static final String THIRDWX_IMAGE_PREFIX = "thirdwx.qlogo.cn";
    //QQ头像前缀
    public static final String THIRDQQ_IMAGE_PREFIX = "thirdqq.qlogo.cn";
    //阿里云oss图片缩略图后缀
    public static final String OSS_IMAGE_THUMB_SUFFIX = "?x-oss-process=image/resize,w_";

    /**
     * 是否发布环境
     *
     * @return
     */
    public static boolean isReleaseEnv() {
        return UrlConfig.BASE_URL.startsWith("http://47.106.62.10");
    }

    /**
     * 从arrays里读取drawable数组资源
     *
     * @param resId
     * @return
     */
    public static int[] getArrayDrawableIds(@ArrayRes int resId) {
        TypedArray typedArray = ApUtils.getApp().getResources().obtainTypedArray(resId);
        int length = typedArray.length();
        int[] drawableIds = new int[length];
        for (int i = 0; i < length; i++) {
            drawableIds[i] = typedArray.getResourceId(i, 0);
        }
        typedArray.recycle();
        return drawableIds;
    }

    /**
     * 转换年龄显示
     *
     * @param age
     * @return
     */
    public static String convertAgeString(int age) {
        return age > 0 ? age + "岁" : PZH;
    }

    /**
     * 转换省市区
     *
     * @param province
     * @param city
     * @return
     */
    public static String convertProvinceCityString(String province, String city) {
        if (TextUtils.isEmpty(province)) {
            if (TextUtils.isEmpty(city)) {
                return "--";
            }
            return city;
        } else {
            if (TextUtils.isEmpty(city)) {
                return province;
            }
            return province + " " + city;
        }
    }

    /**
     * 转换市区
     *
     * @param city
     * @return
     */
    public static String convertCityString(String city) {
        return TextUtils.isEmpty(city) ? PZH : city;
    }

    /**
     * 是否为男
     *
     * @param str
     * @return
     */
    public static boolean isMaleStr(String str) {
        return "男".equals(str) || String.valueOf(DataType.SEX_MALE).equals(str);
    }

    /**
     * 是否为女
     *
     * @param str
     * @return
     */
    public static boolean isFemaleStr(String str) {
        return "女".equals(str) || String.valueOf(DataType.SEX_FEMALE).equals(str);
    }

    /**
     * 性别转换文本
     *
     * @param sex
     * @return
     */
    public static String convertSexString(int sex) {
        return sex == 2 ? "女" : "男";
    }

    /**
     * 格式化pv数量
     *
     * @param count
     * @return
     */
    public static String formatPageCount(int count) {
        if (count >= 100000) {
            return String.format("%.1fw", count / 100000f);
        } else if (count >= 10000) {
            return String.format("%.1fw", count / 10000f);
        } else if (count >= 1000) {
            return String.format("%.1fk", count / 1000f);
        } else {
            return count + "";
        }
    }

    /**
     * 根据文章类型得到相应的点赞类型
     *
     * @param articleType
     * @param isComment
     * @return
     */
    public static int convertArticleType2PraiseType(int articleType, boolean isComment) {
        switch (articleType) {
            case DataType.POST_TOPIC:
                return isComment ? DataType.PRAISE_TOPIC_COMMENT : DataType.PRAISE_TOPIC;
            case DataType.POST_DYNAMIC:
                return isComment ? DataType.PRAISE_DYNAMIC_COMMENT : DataType.PRAISE_DYNAMIC;
            case DataType.POST_ASK:
                return isComment ? DataType.PRAISE_ASK_COMMENT : DataType.PRAISE_ASK;
        }
        return 0;
    }


    /**
     * 根据图片路径及最大宽度
     * 转换为阿里oss缩略图
     *
     * @param url
     * @return
     */
    public static String getOssThumbImageUrl(String url, int width) {
        if (url == null) {
            return "";
        }
        if (!url.contains(OSS_IMAGE_PREFIX) && !url.contains(OSS_IMAGE_PREFIX_OLD)) {
            return url;
        }
        return String.format("%s%s%s", url, OSS_IMAGE_THUMB_SUFFIX, width);
    }

    /**
     * 根据缩略图得到全平台原图路径
     *
     * @param url
     * @return
     */
    public static String getFullPlatformOriginImageUrl(String url) {
        if (url.contains(THIRDWX_IMAGE_PREFIX)) {
            if (url.endsWith("/132") || url.endsWith("/96")
                    || url.endsWith("/64") || url.endsWith("/46")) {
                return url.substring(0, url.lastIndexOf("/")) + "/0";
            }
            return url;
        }
//        else if (url.contains(THIRDQQ_IMAGE_PREFIX)) {
//            //不能直接拼0，有可能是企鹅
//            return url.substring(0, url.lastIndexOf("&s=")) + "&s=0";
//        }
        else {
            int index = url.indexOf(OSS_IMAGE_THUMB_SUFFIX);
            if (index == -1) {
                return url;
            }
            return url.substring(0, index);
        }
    }

    /**
     * 根据缩略图得到原图路径
     *
     * @param url
     * @return
     */
    public static String getFullPlatformThumbImageUrl(String url, int width) {
        if (url.contains(THIRDWX_IMAGE_PREFIX)) {
            if (url.endsWith("/132") || url.endsWith("/96")
                    || url.endsWith("/64") || url.endsWith("/46")) {
                return url.substring(0, url.lastIndexOf("/")) + "/0";
            }
            return url;
        }
//        else if (url.contains(THIRDQQ_IMAGE_PREFIX)) {
//            //不能直接拼0，有可能是企鹅
//            return url.substring(0, url.lastIndexOf("&s=")) + "&s=0";
//        }
        else {
            if (url.contains(OSS_IMAGE_PREFIX) || url.contains(OSS_IMAGE_PREFIX_OLD)) {
                return String.format("%s%s%s", url, OSS_IMAGE_THUMB_SUFFIX, width);
            }
            return url;
        }
    }

    /**
     * 是否有圈子的操作权限
     *
     * @param sex
     * @param permission
     * @return
     */
    public static boolean hasCirclePermission(int sex, int permission) {
        return (sex == DataType.SEX_MALE && (permission == DataType.CIRCLE_PERMISSION_MALE
                || permission == DataType.CIRCLE_PERMISSION_MALE_MALE || permission == DataType.CIRCLE_PERMISSION_OPEN))
                || (sex == DataType.SEX_FEMALE && (permission == DataType.CIRCLE_PERMISSION_FEMALE
                || permission == DataType.CIRCLE_PERMISSION_FEMALE_FEMALE || permission == DataType.CIRCLE_PERMISSION_OPEN));
    }

    /**
     * 是否公开圈
     *
     * @param permission
     * @return
     */
    public static boolean isCircleOpenPermission(int permission) {
        return permission == DataType.CIRCLE_PERMISSION_OPEN;
    }

    /**
     * 是否完善标签资料
     *
     * @return
     */
    public static boolean isLabelInfoImPerfect() {
        UserInfoResponse userInfo = RcUser.getInstance().getInfoResponse();
        if (userInfo != null) {
            return TextUtils.isEmpty(userInfo.getMylabelIds())
                    || TextUtils.isEmpty(userInfo.getIdealObjectIds());
        }
        return true;
    }

    /**
     * 添加全部圈子项至首部
     *
     * @param coterieItemList
     */
    public static List<CoterieItem> addAll2CoterieList(List<CoterieItem> coterieItemList) {
        if (coterieItemList == null) {
            coterieItemList = new ArrayList<>();
        }
        coterieItemList.add(0, getAllTypeCoterieItem());
        return coterieItemList;
    }

    /**
     * 圈子名为全部的圈子
     *
     * @return
     */
    public static CoterieItem getAllTypeCoterieItem() {
        CoterieItem allItem = new CoterieItem();
        allItem.setCoteriePkId("");
        allItem.setCoterieName(ApUtils.getApp().getString(R.string.all));
        return allItem;
    }

    /**
     * 获取raw目录下的媒体资源
     *
     * @param resid
     * @return
     */
    public static String getRawResouceString(int resid) {
        return "android.resource://" + ApUtils.getApp().getPackageName() + "/raw/" + resid;
    }

}
